package com.yunc.modules.shop.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunc.base.controller.CommonController;
import com.yunc.common.utils.R;
import com.yunc.modules.shop.entity.ItemSpecs;
import com.yunc.modules.shop.service.IItemSpecsService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户管理
 *
 * @author congcong tao
 * @date 2018/12/24 9:30
 */
@RestController
@RequestMapping("itemSpecs")
public class ItemSpecsController extends CommonController {

    private static final Logger logger = LogManager.getLogger(ItemSpecsController.class);

    @Autowired
    private IItemSpecsService itemSpecsService;
    
    @GetMapping("/page")
    public R page(Page<ItemSpecs> page, ItemSpecs itemSpecs){
        itemSpecsService.page(page, new QueryWrapper<>(itemSpecs));
        return R.ok().put("data", page.getRecords()).put("count", page.getTotal());
    }

    @GetMapping("/list")
    public R list(ItemSpecs itemSpecs){
        QueryWrapper<ItemSpecs> ew = new QueryWrapper<>();
        List<ItemSpecs> list = itemSpecsService.list(ew);
        return R.ok().put("list", list).put("itemSpecs", itemSpecs);
    }

    @GetMapping("/info/{id}")
    public R info(@PathVariable String id){
        ItemSpecs itemSpecs = itemSpecsService.getById(id);
        return R.ok().put("itemSpecs", itemSpecs);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody ItemSpecs itemSpecs){
        boolean flag = itemSpecsService.save(itemSpecs);
        if (flag) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     */
    @PutMapping(value = "/update")
    public R update(@RequestBody ItemSpecs itemSpecs){
        boolean flag = itemSpecsService.updateById(itemSpecs);
        if (flag) {
            return R.ok();
        }
        return R.error();
    }
}
